package com.wg.result;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wg.emu.CodeEnum;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 平台统一返回数据格式
 * @Date 2017/7/26
 */

public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    @ApiModelProperty("是否正确")
    private boolean success = true;
    @ApiModelProperty("状态码")
    private Integer code = CodeEnum.SUCCESS.getCode();
    @ApiModelProperty("返回包含的信息")
    private String msg = "";

    @ApiModelProperty("返回包含的对象")
    private T data ;

    /**
     * 失败,无信息返回
     * @return
     */
    public static <T> ResponseResult<T> toFail() {
        ResponseResult response = new ResponseResult(false, CodeEnum.FAIL.getCode());
        return response;
    }

    /**
     * 失败,并返回错误信息
     * @param msg 错误信息
     * @return
     */
    public static ResponseResult toFail(String msg) {
        ResponseResult response = new ResponseResult(false, CodeEnum.FAIL.getCode(), msg);
        return response;
    }

    /**
     * 失败,并返回错误信息和错误码\
     * @param code 错误码
     * @param msg 错误信息
     *
     * @return
     */
    public static ResponseResult toFail(Integer code, String msg) {
        ResponseResult response = new ResponseResult(false, code, msg);
        return response;
    }

    /**
     * 失败,参数不合法
     * @return
     */
    public static ResponseResult toParamInvalidateJson() {
        ResponseResult response = new ResponseResult(false,
                CodeEnum.PARAM_INVALIDATE.getCode(), CodeEnum.PARAM_INVALIDATE.getMsg());
        return response;
    }

    /**
     * 成功无信息返回
     * @return
     */
    public static ResponseResult toSuccess() {
        ResponseResult response = new ResponseResult(true, CodeEnum.SUCCESS.getCode());
        return response;
    }

    /**
     * 成功,并放回成功信息
     * @param data 成功数据
     * @return
     */
    public static ResponseResult toSuccess(Object data) {
        ResponseResult response = new ResponseResult(true, CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), data);
        return response;

    }

    /**
     * 成功,并返回数据
     * @param msg 成功信息
     * @param data 成功数据
     * @return
     */
    public static String toSuccess(String msg, Object data) {
        ResponseResult response = new ResponseResult(true, CodeEnum.SUCCESS.getCode(), msg, data);
        return response.toJson();

    }


    public String toJson() {
        if (this.code == null) {
            if (this.success) {
                this.code = CodeEnum.SUCCESS.getCode();
            } else {
                this.code = CodeEnum.FAIL.getCode();
            }
        }
        if (this.msg == null) {
            this.msg = "";
        }
        if (this.data == null) {
            this.data = (T) new Object();
        }
        return JSON.toJSONString(this, SerializerFeature.BrowserCompatible, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 重置属性值
     */
    public void reset(boolean success, Integer code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    /**
     * 重置属性值
     */
    public void reset(boolean success, Integer code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 重置属性值
     */
    public void resetFail(String msg) {
        this.success = false;
        this.code = CodeEnum.FAIL.getCode();
        this.msg = msg;
    }

    /**
     * 重置属性值
     */
    public void resetSuccess(String msg) {
        this.success = true;
        this.code = CodeEnum.SUCCESS.getCode();
        this.msg = msg;
    }

    /**
     * 重置属性值
     */
    public void resetSuccess(T data) {
        this.success = true;
        this.code = CodeEnum.SUCCESS.getCode();
        this.msg = CodeEnum.SUCCESS.getMsg();
        this.data = data;
    }

    /**
     * 重置属性值
     */
    public void reset(T data) {
        this.data = data;
    }

    public ResponseResult() {

    }

    public ResponseResult(boolean success, Integer code) {
        this.success = success;
        this.code = code;
    }

    public ResponseResult(boolean success, Integer code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(boolean success, Integer code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
